home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / steng < prev    next >
Text File  |  1995-03-20  |  3KB  |  97 lines

  1. //-------------------------------------------------------------------//
  2. // Synopsis: Short-Time ENerGy.
  3.  
  4. // Syntax: </ TSCALE ; Y /> = sp_steng(X,FRAME,OVERLAP,FS,WIN)
  5.  
  6. // Description:
  7.  
  8. //      steng computes the short-time energy of X using a size FRAME
  9. //      (seconds) and a percentage OVERLAP (%) between successive
  10. //      frames using a rectangular data window.  The sampling
  11. //      frequency is given by FS (samples/second). The short-time
  12. //      energy curve is returned in Y and a time scale corresponding
  13. //      to the end of the data frame is returned in TSCALE.  The curve
  14. //      may be displayed with the command `plot([tscale,y])'.
  15.  
  16. //      The last, and optional argument, WIN, specifies the data
  17. //      window used before computing the short-time energy.  WIN can
  18. //      be one of the following:
  19.  
  20. //          hamming    "hamm"
  21. //          hanning    "hann"
  22. //          blackman   "blac"
  23. //          triangular "tria"
  24.  
  25. //      See also: SP_STENG, SP_STZCR, AVSMOOTH, MDSMOOTH
  26.  
  27. //      Matlab original by:
  28. //       LT Dennis W. Brown 7-11-93, DWB 11-11-94
  29. //       Naval Postgraduate School, Monterey, CA
  30. //       May be freely distributed.
  31. //       Not for use in commercial products.
  32.  
  33. //       Ref: Rabiner & Schafer, Digital Processing of Speech
  34. //       Signals, 1978, ss 4.2, pp 120-126.
  35.  
  36. //-------------------------------------------------------------------//
  37.  
  38. require window
  39.  
  40. steng = function (x, frame, overlap, fs, win)
  41. {
  42.   local (x, frame, overlap, fs, win)
  43.  
  44.   // default values
  45.   y = [];
  46.   tscale = [];
  47.  
  48.   // must have at least 3 args
  49.   if (nargs < 4)
  50.   {
  51.     error("sp_steng: Requires first three arguments.");
  52.   }
  53.  
  54.   // percentage must be in range 0-100
  55.   if (overlap < 0 || overlap > 100)
  56.   {
  57.     error("sp_steng: Overlap percentage must be in range 0-100%");
  58.   }
  59.  
  60.   // figure out if we have a vector
  61.   if (min(size(x)) != 1)
  62.   {
  63.     error("sp_steng: Input arg \"x\" must be a 1xN or Nx1 vector.");
  64.   }
  65.  
  66.   // work with Nx1 vectors
  67.   x = x[:];
  68.   // variables
  69.   Ns = length(x);                         // number of samples
  70.   N = floor(fs * frame);                  // samples-per-frame
  71.   Ndiff = floor(N * (1 - overlap/100));   // samples between windows
  72.   L = floor((Ns-N)/Ndiff);                // number of windows
  73.   y = zeros(L,1);                         // space for answer
  74.   tscale = zeros(L,1);                    // space for indices
  75.  
  76.   // data window
  77.   datawindow = ones(N,1);                 // rectangular default
  78.   if (nargs == 5)
  79.   {
  80.     datawindow = window (N, win);
  81.   }
  82.  
  83.   // square the data and the window
  84.   datawindow = datawindow .^ 2;
  85.   x = x .^ 2;
  86.  
  87.   // windows with overlap
  88.   for (k in 1:L)
  89.   {
  90.     s1 = (k-1) * Ndiff + 1;        // start of window
  91.     tscale[k;1] = k * Ndiff/fs;
  92.     y[k;1] = sum(x[s1:s1+N-1;1] .* datawindow);
  93.   }
  94.  
  95.   return << y=y; tscale=tscale >>;
  96. };
  97.